﻿using Model;
using System.Data;

namespace BLL
{
    public class Sys_RoleModuleBLL : BaseBLL
    {
        public DataTable findByRole(string id)
        {
            string sql = "SELECT * FROM Sys_RoleModule WHERE ROLE_ID in (" + id + ")";
            return Query(sql);
        }

        public DataTable findByRoleAndModule(string realName, string rid)
        {
            string sql = "SELECT BUTTONS FROM Sys_RoleModule rm,SYS_MODULE m WHERE rm.MODULE_ID=m.MODULE_ID AND rm.ROLE_ID in (" + rid + ") AND m.MODULE_PATH like '%" + realName + "'";
            return Query(sql);
        }

        public int INSERT(Sys_RoleModule model, string operName)
        {
            string sql = "SELECT * FROM Sys_RoleModule WHERE ROLE_ID=" + model.ROLE_ID + " AND MODULE_ID=" + model.MODULE_ID;
            if (GetCount(sql) <= 0)
            {
                sql = string.Format(@"INSERT INTO Sys_RoleModule(ROLE_ID,MODULE_ID) VALUES({0},{1})", model.ROLE_ID, model.MODULE_ID);
                int count = NoQuery(sql, operName);
                return count;
            }
            return 0;
        }

        public int UPDATE(Sys_RoleModule model, string operName)
        {
            string sql = string.Format(@"UPDATE SYS_ROLEMODULE SET MODULE_ID='{0}' WHERE ROLE_ID='{1}'", model.MODULE_ID, model.ROLE_ID);
            int count = NoQuery(sql, operName);
            return count;
        }

        public int DELETE(string ids, string ModuleId, string operName)
        {
            string sql = "DELETE SYS_ROLEMODULE WHERE ROLE_ID=" + ids;

            if (!string.IsNullOrEmpty(ModuleId))
            {
                if (ModuleId.Length - 1 >= 1)
                {
                    sql += " AND  MODULE_ID  IN (SELECT MODULE_ID FROM SYS_MODULE WHERE MODULE_ID NOT IN(" + ModuleId.Substring(0, ModuleId.Length - 1) + "))";
                }
            }
            int count = NoQuery(sql, operName);
            return count;
        }
    }
}